home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 201-225 / disk_214 / memdiag / mq.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  98 lines

  1. /* MQ.c  Memory Quarantine
  2.  
  3.    by Fabbian G. Dufoe, III
  4.  
  5.    This program walks the memory free list.  If it finds memory on the free
  6.    list which has been identified as defective by MD (Memory Diagnostic) it
  7.    patches the memory free list so that memory will appear to be already
  8.    allocated.  The memory cannot be recovered until the system is restarted.
  9.  
  10.    The program begins by getting a pointer to the system's memory free list.
  11.    For each entry in the list it compares the starting and ending addresses
  12.    of the free memory block against the addresses of the defective memory.
  13.    If a defective memory address falls within the free block MQ changes the
  14.    entry for the free block so the free block will appear to end just short
  15.    of the defective address.  Then it adds a new entry to the list.  The new
  16.    entry identifies a free block beginning just after the defective address
  17.    and continuing to the point where the original block ended.  This process
  18.    is repeated for each defective address and for each free block.
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <exec/types.h>
  23. #include <exec/exec.h>
  24. #include <exec/execbase.h>
  25.  
  26. struct ExecBase *ExecBase;
  27.  
  28. void
  29. main()
  30. {
  31.    ULONG BadAddr[100];
  32.    int bytes = 0;
  33.    int i;
  34.    int last = 0;
  35.    struct MemChunk *LastChunk = NULL;
  36.    struct MemChunk *MemChunk;
  37.    struct MemHeader *MemHeader;
  38.  
  39.    while ((scanf("%8lX\n", &BadAddr[last++]) != EOF) && (last < 100));
  40.    last--;
  41.  
  42.    if ((ExecBase = (struct ExecBase *)
  43.                    OpenLibrary("exec.library", 0L)) == NULL)
  44.       exit(20);
  45.  
  46.    Forbid();
  47.    MemHeader = (struct MemHeader *)ExecBase->MemList.lh_Head;
  48.  
  49.    while(MemHeader->mh_Node.ln_Succ)
  50.    {
  51.       for (MemChunk = MemHeader->mh_First; MemChunk;
  52.             MemChunk = MemChunk->mc_Next)
  53.       {
  54.          for (i = 0; i < last; i++)
  55.          {
  56.             struct MemChunk *NewChunk;
  57.             ULONG Size1;
  58.             ULONG Size2;
  59.  
  60.             /* If chunk ends before bad address get next chunk. */
  61.             if ((ULONG)MemChunk + MemChunk->mc_Bytes - 1 < BadAddr[i])
  62.                break;
  63.             /* If chunk begins after bad address get next address. */
  64.             if ((ULONG)MemChunk > BadAddr[i])
  65.                continue;
  66.             /* Bad address falls within chunk. */
  67.             Size1 = BadAddr[i] - (ULONG)MemChunk;
  68.             Size1 = Size1 - Size1 % 8;
  69.             Size2 = MemChunk->mc_Bytes - Size1 - 8;
  70.             NewChunk = (struct MemChunk *)((ULONG)MemChunk + Size1 + 8);
  71.             if (Size2 == 0)
  72.                MemChunk->mc_Bytes = Size1;
  73.             else
  74.             {
  75.                NewChunk->mc_Next = MemChunk->mc_Next;
  76.                NewChunk->mc_Bytes = Size2;
  77.                MemChunk->mc_Next = NewChunk;
  78.                MemChunk->mc_Bytes = Size1;
  79.             }
  80.             if (Size1 == 0)
  81.             {
  82.                if (LastChunk == NULL)
  83.                   MemHeader->mh_First = MemChunk->mc_Next;
  84.                else
  85.                   LastChunk->mc_Next = MemChunk->mc_Next;
  86.             }
  87.             MemHeader->mh_Free -= 8;
  88.             bytes += 8;
  89.          }
  90.          LastChunk = MemChunk;
  91.       }
  92.       MemHeader = (struct MemHeader *)MemHeader->mh_Node.ln_Succ;
  93.    }
  94.    Permit();
  95.    printf("MQ: %d bytes quarantined.\n", bytes);
  96.    return;
  97. }
  98.